package cn.schoolwow.workflow.test.module.assigner.testcase;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.query.jsonarray.QueryJSONArray;
import cn.schoolwow.util.domain.query.resource.QueryResource;
import cn.schoolwow.workflow.QuickWorkFlow;
import cn.schoolwow.workflow.domain.instance.StartWorkFlowRequest;
import cn.schoolwow.workflow.domain.instance.WorkFlowInstanceStatus;
import cn.schoolwow.workflow.domain.task.TaskType;
import cn.schoolwow.workflow.domain.task.WorkFlowAssigner;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskQuery;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskResponse;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.Validate;
import org.junit.Assert;

import java.util.List;

public class StartStartProjectAuditTestCaseFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        setNodes(flowContext);
        startWorkFlow(flowContext);
        technologyAudit(flowContext);
        financeAudit(flowContext);
        generalAudit(flowContext);
        checkStatus(flowContext);
    }

    @Override
    public String name() {
        return "启动项目多人会签测试用例";
    }

    private void setNodes(FlowContext flowContext){
        JSONObject workflow = QueryResource.newQuery("启动项目工作流部署文件", "workflow/多审批人/项目启动会签审批.json")
                .execute()
                .getAsJSONObject();
        JSONArray nodes = workflow.getJSONArray("nodes");
        flowContext.putCurrentFlowData("nodes", nodes);
    }

    private void startWorkFlow(FlowContext flowContext){
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);

        StartWorkFlowRequest startWorkFlowRequest = new StartWorkFlowRequest();
        startWorkFlowRequest.definitionName = "项目启动会签审批";
        startWorkFlowRequest.initiator = "张三";

        WorkFlowInstance workFlowInstance = quickWorkFlow.startWorkFlow(startWorkFlowRequest);
        flowContext.putInstanceData(workFlowInstance);

        WorkFlowTaskQuery workFlowTaskQuery = new WorkFlowTaskQuery();
        workFlowTaskQuery.instanceIdList.add(workFlowInstance.getId());
        flowContext.putCurrentFlowInstanceData(workFlowTaskQuery);
    }

    private void technologyAudit(FlowContext flowContext){
        JSONArray nodes = flowContext.checkData("nodes", JSONArray.class);
        WorkFlowTaskQuery workFlowTaskQuery = flowContext.checkInstanceData(WorkFlowTaskQuery.class);
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);

        JSONObject node = QueryJSONArray.newQuery(nodes)
                .addQuery("name", "技术评审")
                .execute()
                .getObject();
        JSONArray assigner = node.getJSONArray("assigner");
        List<WorkFlowAssigner> workFlowAssignerList = assigner.toJavaList(WorkFlowAssigner.class);
        for(int i=workFlowAssignerList.size()-1;i>=0;i--){
            WorkFlowAssigner workFlowAssigner = workFlowAssignerList.get(i);

            for(int j=0;j<1;j++){
                workFlowTaskQuery.assignerList.clear();
                workFlowTaskQuery.assignerList.add(workFlowAssigner.assignerList.get(j));
                WorkFlowTaskResponse workFlowTaskResponse = quickWorkFlow.getSingleWorkFlowTask(workFlowTaskQuery);
                Validate.notNull(workFlowTaskResponse, "审批人["+workFlowAssigner.assignerList.get(j)+"]的任务不存在!");
                quickWorkFlow.completeTask(workFlowTaskResponse.taskId, TaskType.COMPLETE);
            }
        }
    }

    private void financeAudit(FlowContext flowContext){
        JSONArray nodes = flowContext.checkData("nodes", JSONArray.class);
        WorkFlowTaskQuery workFlowTaskQuery = flowContext.checkInstanceData(WorkFlowTaskQuery.class);
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);

        JSONObject node = QueryJSONArray.newQuery(nodes)
                .addQuery("name", "财务评审")
                .execute()
                .getObject();
        JSONArray assigner = node.getJSONArray("assigner");
        List<WorkFlowAssigner> workFlowAssignerList = assigner.toJavaList(WorkFlowAssigner.class);
        for(int i=0;i<workFlowAssignerList.size();i++){
            WorkFlowAssigner workFlowAssigner = workFlowAssignerList.get(i);

            for(String innerAssigner:workFlowAssigner.assignerList){
                workFlowTaskQuery.assignerList.clear();
                workFlowTaskQuery.assignerList.add(innerAssigner);
                WorkFlowTaskResponse workFlowTaskResponse = quickWorkFlow.getSingleWorkFlowTask(workFlowTaskQuery);
                Validate.notNull(workFlowTaskResponse, "审批人["+innerAssigner+"]的任务不存在!");
                quickWorkFlow.completeTask(workFlowTaskResponse.taskId, TaskType.COMPLETE);
            }
        }
    }

    private void generalAudit(FlowContext flowContext){
        JSONArray nodes = flowContext.checkData("nodes", JSONArray.class);
        WorkFlowTaskQuery workFlowTaskQuery = flowContext.checkInstanceData(WorkFlowTaskQuery.class);
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);

        JSONObject node = QueryJSONArray.newQuery(nodes)
                .addQuery("name", "综合评审")
                .execute()
                .getObject();
        JSONArray assigner = node.getJSONArray("assigner");
        List<WorkFlowAssigner> workFlowAssignerList = assigner.toJavaList(WorkFlowAssigner.class);
        for(int i=0;i<workFlowAssignerList.size();i++){
            WorkFlowAssigner workFlowAssigner = workFlowAssignerList.get(i);
            for(String innerAssigner:workFlowAssigner.assignerList){
                workFlowTaskQuery.assignerList.clear();
                workFlowTaskQuery.assignerList.add(innerAssigner);
                WorkFlowTaskResponse workFlowTaskResponse = quickWorkFlow.getSingleWorkFlowTask(workFlowTaskQuery);
                Validate.notNull(workFlowTaskResponse, "审批人["+innerAssigner+"]的任务不存在!");
                quickWorkFlow.completeTask(workFlowTaskResponse.taskId, TaskType.COMPLETE);
            }
        }
    }

    private void checkStatus(FlowContext flowContext){
        WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);

        WorkFlowInstance workFlowInstance1 = quickWorkFlow.getSingleWorkFlowInstance(workFlowInstance.getId());
        Assert.assertEquals(WorkFlowInstanceStatus.Finished.status, workFlowInstance1.getStatus().intValue());
    }

}
